﻿//$(function () {
//    $("select").select2();
//    $("input:submit").fastClick(function () {
//        if ($(this).hasClass("disabled")) return false;
//        else $(this).addClass("disabled");
//    });
//})
function instance_initializeRequest(sender, args) {
    if (!$("form").validate().form()) {
        args.set_cancel(true);
    }
}
var $pickerAll;
$(function () {
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(instance_initializeRequest);//jquery.validate和asp:UpdatePanel共存，验证失败后页面不postback
    
    //$("select").not("[data-noselect2]").select2();
    //$("form").validate();
    $("input[name=tag]:visible,[tag]:visible").tagsInput();
    //var $pickerAll = $('.datepicker').pickadate();
    
    try { page_default_picker(); } catch (e) { }
    if ($('.datepicker').length > 0) {
        var pickerAll = $pickerAll.pickadate('picker');
        pickerAll.on("open", function () {
            $(".picker").width(this.$node.outerWidth());
        })
    }
    //
    var $pickerFrom = $('.datepicker.from').pickadate();
    if ($pickerFrom.length > 0) {
        var pickerFrom = $pickerFrom.pickadate('picker');
        pickerFrom.on("close", function () {
            if (this.get() != "") {
                var $pickerTo = $(".datepicker[id$=" + this.$node.attr("to") + "]").pickadate();
                var pickerTo = $pickerTo.pickadate("picker");
                pickerTo.set("min", this.get("select")).set('select', this.get("select"));
                setTimeout(function () {
                    pickerTo.open();
                }, 500);
            }
        })
    }
    //单张图片选择
    $(".CkFinderSinglePicOpener").each(function () {
        var _input = $("[id$=" + $(this).attr("data-target") + "]");
        //删除
        var _deler = $("<i class=\"picdeler fa fa-times-circle hide\"></i>").fastClick(function () {
            _input.val(""); _img.attr("src", "").hide(); $(this).hide();
        });
        //图片
        var _src = "/img/blank.gif";
        if (_input.val() != "") {
            _src = _input.val().replace("UploadFiles", "UploadFiles/_thumbs");
            _deler.show();
        }
        var _img = $("<img src=\"" + _src + "\"></img>");
        //
        var ul_Pic=$("<ul class=ul_Pic></ul>"),li_Pic=$("<li></li>");
        _img.add(_deler).appendTo(li_Pic);
        li_Pic.appendTo(ul_Pic);
        ul_Pic.appendTo(_input.parent());
        
    }).fastClick(function () {
        var openerHtml = $(this).find(".OpenerHtml");
        if (openerHtml.html() == "点击选择图片") {
            openerHtml.html("点击选择图片");

            parent.ckfinderImgGetter_Input = $("[id$=" + $(this).attr("data-target") + "]");
            parent.ckfinderImgGetter_Dom = parent.ckfinderImgGetter_Input.siblings(".ul_Pic").find("img");
            parent.buildCkFinder(1, "single", "选择"+$(this).attr("data-title")+"图片");
            openerHtml.html("关闭选择窗口");
        } else {
            parent.buildCkFinder(2);
            openerHtml.html("点击选择图片");
        }
        return false;
    })
    //单张资源选择
    $(".CkFinderSingleMediaOpener").each(function () {
        var _input = $("[id$=" + $(this).attr("data-target") + "]");
        
        
    }).fastClick(function () {
        var openerHtml = $(this).find(".OpenerHtml");
        var mediaTitle = $(this).attr("data-title");
        if (openerHtml.html() == "点击选择" + mediaTitle) {

            parent.ckfinderImgGetter_Input = $("[id$=" + $(this).attr("data-target") + "]");
            parent.ckfinderImgGetter_Dom = parent.ckfinderImgGetter_Input.siblings(".ul_Pic").find("img");
            parent.buildCkFinder(1, "single", "选择" + mediaTitle + "",false);
            openerHtml.html("关闭选择窗口");
        } else {
            parent.buildCkFinder(2);
            openerHtml.html("点击选择" + mediaTitle);
        }
        return false;
    })
    //
    //if ($(".ckeditor").length > 0) {
    //    FormCkeditor = CKEDITOR.replace($(".ckeditor").attr("id"));
    //    CKEDITOR.instances[$(".ckeditor").attr("id")].on("instanceReady", function () {//为编辑器设置事件
    //        $(".cke_button:last").click(function () {
    //            //alert("ddd");
    //            if ($(this).hasClass("cke_button_on")) {
    //                parent.full_adminRight(true);
    //                // $("#div_slickupload ").hide();
    //            }
    //            else {
    //                parent.full_adminRight(false);
    //                // $("#div_slickupload ").show();
    //            }

    //        })
    //    });
    //}
})
function submit_click(w_) {
    try { after_submit_click(); } catch (e) { }
    $(".ckeditor").each(function () {
        $(this)
            //.show().css({ visibility: "" })
            .val(CKEDITOR.instances[$(this).attr("id")].getData());
    })
    w_.disabled = true;
}
function BuildMultiPic(src_) {
    //src_ = src_.replace("UploadFiles", "UploadFiles/_thumbs");
    var src_thumbs = src_.replace("UploadFiles", "UploadFiles/_thumbs");
    if ($("#ul_Pic img:last").attr("src") == src_thumbs) return;
    var maybePic = $("#ul_Pic img[src='" + src_ + "']");
    if (maybePic.length > 0) delPic(maybePic[0]);
    //
    var newpic = $("#template_pic li:first").clone(true);
    newpic.find("[name=url]").val(src_);
    newpic.find("img").attr("src", src_thumbs);
    newpic.html(newpic.html().replace(/{{URL}}/g, src_));
    // var newpic = $("<li ><i class=\"picdeler icon-remove\" onclick=\"delPic(this)\"></i><img src=\"" + src_ + "\"><input type=\"hidden\"  name=\"picurl\"  value=\"" + src_ + "\"><input type=\"hidden\"  name=\"picid\"  value=\"0\"></li>");
    newpic.hide().appendTo("#ul_Pic").slideDown("fast", function () {
        newpic.find("input[name=tag]").tagsInput();
        $('html, body, .content').animate({ scrollTop: $(document).height() }, 300);
    });

    //if ($("#ul_Pic [name=iscover]").length == 1) $("#ul_Pic [name=iscover]").attr("checked", "checked");
}
function delPic(w_) {
    $(w_).parents("li").animate({ opacity: 0, height: 0 }, "fast", function () {
        //var thisiscover =( $(this).find("[name=iscover]").attr("checked")=="checked");
        $(this).remove();
       // if ($("#ul_Pic [name=iscover]").length == 1 || thisiscover) $("#ul_Pic [name=iscover]:first").attr("checked", "checked");
        
    })
   
}